package com.login;

import com.config.ReadData;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import static org.junit.Assert.assertEquals;


@RunWith(Parameterized.class)
public class TestLogin {

    private static WebDriver d;
    private String username;
    private String password;
    private String result;

    public TestLogin(String username, String password, String result) {
        this.username = username;
        this.password = password;
        this.result = result;
    }

    @Before
    public void setUp() throws Exception {
        System.setProperty("webdriver.gecko.driver", "F:\\python\\geckodriver.exe");
        d = new FirefoxDriver();

    }

    @Parameterized.Parameters
    public static Collection<String[]> perpareData() throws IOException {
        ArrayList<String[]> params = new ArrayList<>();
        ReadData rd = new ReadData("src/com/config/login.csv");
        params.addAll(rd.readcsvData(3));
        return params;
    }

    /**
     * 测试登录
     */
    @Test
    public void testLogin() throws InterruptedException {
        d.get("http://127.0.0.1:8080/login.jsp");
        d.findElement(By.id("username")).sendKeys(username);
        d.findElement(By.id("password")).sendKeys(password);
        d.findElement(By.id("logsub")).click();
        Thread.sleep(50);
        String a=null;
        try{
           a = d.findElement(By.id("wel_login_msg")).getText().trim();
        }catch (Exception e){
            e.printStackTrace();
            a="登录成功";
        }
        assertEquals(a, result);
    }


    @After
    public void tearDown() throws Exception {
        d.quit();
    }


}
